package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Task;
import com.xxx.entity.dto.TaskDto;
import com.xxx.service.ITaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工作任务表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/task")
@Tag(name = "工作任务管理")
public class TaskController {

    @Autowired
    private ITaskService taskService;

    @PostMapping("/add")
    @Operation(summary = "添加工作任务")
    public Result add(@RequestBody Task task){
        Boolean flag = taskService.save(task);
        return flag ? Result.success("添加工作任务成功") : Result.error("添加工作任务失败");
    }

    @GetMapping("/delete/{taskId}")
    @Operation(summary = "删除工作任务")
    public Result delete(@PathVariable Integer taskId){
        Boolean flag = taskService.removeById(taskId);
        return flag ? Result.success("删除工作任务成功") : Result.error("不存在该工作任务Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改工作任务")
    public Result update(@RequestBody Task task){
        Boolean flag = taskService.updateById(task);
        return flag ? Result.success("修改工作任务成功") : Result.error("修改工作任务失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询工作任务")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Task> page = taskService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据任务名、用户Id、任务Id条件查询工作任务")
    public Result selectByCondition(@RequestBody TaskDto taskDto){
        LambdaQueryWrapper<Task> lqw = new LambdaQueryWrapper<>();
        lqw.like(taskDto.getTaskName()!=null&&!taskDto.getTaskName().equals(""),
                        Task::getTaskName,taskDto.getTaskName())
                .eq(taskDto.getUserId()!=null,Task::getUserId,taskDto.getUserId())
                .eq(taskDto.getTaskId()!=null,Task::getTaskId,taskDto.getTaskId());
        IPage<Task> page = taskService.page(new Page<>(taskDto.getCurrent(), taskDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

